//
//  HJGroupSelectMemberCell.m
//  KaiChat
//
//  Created by lx on 2023/11/8.
//

#import "HJGroupSelectMemberCell.h"
#import "HJGradientLabel.h"
#import "HJButton.h"

@interface HJGroupSelectMemberCell()

@property (nonatomic, weak) UIImageView *avatarImageView;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UIImageView *adminIconView;
@property (nonatomic, weak) UIImageView *ownerIconView;
@property (nonatomic, weak) HJButton *infoButton;
@property (nonatomic, weak) UIButton *boxButton;

@end


@implementation HJGroupSelectMemberCell

- (void)settingSubView {
    [super settingSubView];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIButton *boxButton = [UIButton addToView:self.contentView];
    boxButton.userInteractionEnabled = NO;
    [boxButton setBackgroundImage:[UIImage imageNamed:@"box_icon"] forState:UIControlStateNormal];
    [boxButton setBackgroundImage:[UIImage imageNamed:@"box_select_icon"] forState:UIControlStateSelected];
    [boxButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(leftRightMargin);
        make.centerY.offset(0);
        make.width.height.offset(20);
    }];
    
    UIImageView *avatarImageView = [UIImageView addToView:self.contentView];
    avatarImageView.addCornerRadius(20);
    avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(boxButton.mas_right).offset(10);
        make.centerY.offset(0);
        make.width.height.offset(40);
    }];

    UILabel *nameLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(14) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(avatarImageView.mas_right).offset(10);
        make.centerY.offset(0);
        make.right.mas_lessThanOrEqualTo(-16);
    }];
    
    UIImageView *adminIconView = [UIImageView addToView:self.contentView];
    adminIconView.image = [UIImage imageNamed:@"icon_admin"];
    [adminIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel.mas_right).offset(10);
        make.centerY.offset(0);
        make.width.offset(33);
        make.height.offset(14);
    }];
    
    UIImageView *ownerIconView = [UIImageView addToView:self.contentView];
    ownerIconView.image = [UIImage imageNamed:@"icon_owner"];
    [ownerIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel.mas_right).offset(10);
        make.centerY.offset(0);
        make.width.offset(33);
        make.height.offset(14);
    }];
    
    HJButton *infoButton = [HJButton addToView:self.contentView];
    [infoButton setImage:[UIImage imageNamed:@"look_info"] forState:UIControlStateNormal];
    [infoButton addTarget:self action:@selector(didClickInfoButton) forControlEvents:UIControlEventTouchUpInside];
    [infoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(leftRightMargin);
        make.centerY.offset(0);
        make.width.height.offset(36);
    }];
    
    adminIconView.hidden = YES;
    ownerIconView.hidden = YES;
    
    _adminIconView = adminIconView;
    _ownerIconView = ownerIconView;

    _avatarImageView = avatarImageView;
    _nameLabel = nameLabel;
    _infoButton = infoButton;
    _boxButton = boxButton;
}

- (void)didClickInfoButton {
    if (_didClickInfoButtonBlock) {
        _didClickInfoButtonBlock();
    }
}

- (void)setModel:(HJGroupMemberModel *)model {
    _model = model;
        
    
    _boxButton.selected = model.isSelect;

    
    if (model.nicknameAttributedstring) {
        _nameLabel.attributedText = model.nicknameAttributedstring;
    } else {
        _nameLabel.attributedText = nil;
        _nameLabel.text = model.nickname;
    }
        
    _infoButton.hidden = !model.isLookInfo;
    
    if (model.role == 1) {
        _ownerIconView.hidden = NO;
        _adminIconView.hidden = YES;

    } else if (model.role == 2) {
        _adminIconView.hidden = NO;
        _ownerIconView.hidden = YES;
    } else {
        _adminIconView.hidden = YES;
        _ownerIconView.hidden = YES;
    }

    if (isEmptyString(model.avatar)) {
        _avatarImageView.image = [UIImage imageNamed:defaultAvatarName];
    } else {
        [_avatarImageView sd_setImageWithURL:model.avatar.URL placeholderImage:[UIImage imageNamed:defaultAvatarName]];
    }
}
 

@end
